package com.allwees.bs.module.scm.req.query;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.allwees.bs.core.modelbase.req.QueryReq;
import com.allwees.bs.module.core.req.BranchUserQuery;
import com.allwees.bs.module.scm.entity.ScmAllocateOrderSku;
import com.allwees.core.common.constant.EStatus;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * scm 待分配订单 : sku 维度
 *
 * @author mouhn
 * @since 2020-1-2 10:28
 */
@Data
public class ScmAllocateOrderSkuQueryReq extends QueryReq<ScmAllocateOrderSku> implements BranchUserQuery<ScmAllocateOrderSku> {

    private EScmAllocateOrderQueryStatus status;

    private Date orderTakeEffectAtFrom;

    private Date orderTakeEffectAtTo;

    @Override
    public void buildQuery(QueryWrapper<ScmAllocateOrderSku> queryWrapper) {
        queryWrapper.and(StringUtils.isNotBlank(q), wrapper -> {
            wrapper.eq("uuid", q)
                    .or().eq("sku_no", q)
                    .or().eq("sku_custom_no", q)
                    .or().eq("batch_no", q)
                    .or().eq("order_no", q)
                    .or().eq("order_uuid", q);

        });
        buildBranchUserQuery(queryWrapper)
                .in("status", status.entityStatus())
                .ge(orderTakeEffectAtFrom != null, "order_take_effect_at", orderTakeEffectAtFrom)
                .le(orderTakeEffectAtTo != null, "order_take_effect_at", orderTakeEffectAtTo);
    }

    private enum EScmAllocateOrderQueryStatus {
        // 所有、待发货、超时异常、退单、退货
        ALL {
            @Override
            List<Integer> entityStatus() {
                return Arrays.asList(
                        EStatus.NORMAL.value(),
                        EStatus.SELLOUT.value(),
                        EStatus.USED.value(),
                        EStatus.SCM_TIMEOUT.value(),
                        EStatus.SCM_TIMEOUT_REFUND.value()
                );
            }
        },
        WAIT_ALLOCATE {
            @Override
            List<Integer> entityStatus() {
                return Arrays.asList(
                        EStatus.NORMAL.value(),
                        EStatus.SCM_TIMEOUT.value()
                );
            }
        },
        TIME_OUT_EXCE {
            @Override
            List<Integer> entityStatus() {
                return Arrays.asList(
                        EStatus.SCM_TIMEOUT.value()
                );
            }
        },
        SELLOUT {
            @Override
            List<Integer> entityStatus() {
                return Arrays.asList(
                        EStatus.SELLOUT.value()
                );
            }
        },
        REFUND_ORDER {
            @Override
            List<Integer> entityStatus() {
                return Arrays.asList(EStatus.SCM_REFUND_ORDER.value());
            }
        }
        ;

        abstract List<Integer> entityStatus();
    }
}
